package view;

import handler.LoginHandler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class LoginView extends JFrame {

    JLabel nameLabel = new JLabel("教师端", JLabel.CENTER);
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel usernameLabel = new JLabel("用户名");
    JTextField userTxt = new JTextField();//用户名框
    JLabel pwdLabel = new JLabel("密码");
    JPasswordField pwdField = new JPasswordField();//密码框
    JButton loginBtn = new JButton("登录");
    JButton resetBtn = new JButton("重置");


    SystemTray systemTray;
    TrayIcon trayIcon;

    LoginHandler loginHandler;

    public LoginView() {
        super("学生成绩发布系统");

        loginHandler = new LoginHandler(this);

        Container contentPane = getContentPane();
        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        nameLabel.setPreferredSize(new Dimension(0, 80));

        usernameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200, 30));
        loginBtn.setFont(centerFont);
        resetBtn.setFont(centerFont);
        //加入面板
        centerPanel.add(usernameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        centerPanel.add(loginBtn);
        centerPanel.add(resetBtn);
        //点击按钮监听
        loginBtn.addActionListener(loginHandler);
        resetBtn.addActionListener(loginHandler);
        //回车登录
        loginBtn.addKeyListener(loginHandler);

        //布局

        //布局usernameLabel
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(usernameLabel), Spring.width(userTxt)), Spring.constant(20));
        int offsetx = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, usernameLabel, -offsetx, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, usernameLabel, 20, SpringLayout.NORTH, centerPanel);

        //usertxt
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, usernameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, usernameLabel);

        //pwdlabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, usernameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, usernameLabel);

        //pwdFiled
        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);

        //loginbtn
        springLayout.putConstraint(SpringLayout.WEST, loginBtn, 50, SpringLayout.WEST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 20, SpringLayout.SOUTH, pwdLabel);

        //
        springLayout.putConstraint(SpringLayout.WEST, resetBtn, 50, SpringLayout.EAST, loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH, resetBtn, 0, SpringLayout.NORTH, loginBtn);


        contentPane.add(nameLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);


//        if (SystemTray.isSupported()) {
//            systemTray = SystemTray.getSystemTray();
//            URL imgUrl = LoginView.class.getClassLoader().getResource("小图标.jpg");
//            //trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
//            //设置托盘图片大小自动缩放
//            //trayIcon.setImageAutoSize(true);
//            try {
//                systemTray.add(trayIcon);
//            } catch (AWTException e) {
//                throw new RuntimeException(e);
//            }
//            //增加最小化托盘图标
//            this.addWindowListener(new WindowAdapter() {
//                @Override
//                public void windowIconified(WindowEvent e) {
//                    //super.windowIconified(e);
//                    LoginView.this.dispose();
//                }
//            });
//            //托盘事件监听
//            trayIcon.addMouseListener(new MouseAdapter() {
//                @Override
//                public void mouseClicked(MouseEvent e) {
//                    //super.mouseClicked(e);
//                    int clickCount = e.getClickCount();
//                    if (clickCount == 1) {
//                        LoginView.this.setExtendedState(JFrame.NORMAL);
//                    }
//                    LoginView.this.setVisible(true);
//                }
//            });
//
//        }


        //设置回车默认按钮
        getRootPane().setDefaultButton(loginBtn);

//        加入图片
        //URL imgurl = LoginView.class.getClassLoader().getResource("小图标.jpg");
        //setIconImage(new ImageIcon(imgurl).getImage());
        setSize(600, 400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new LoginView();
    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

}
